package com.eshop.userorder.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.eshop.bean.AddressBean;
import com.eshop.bean.CartBean;
import com.eshop.bean.GoodsBean;
import com.eshop.dao.Conn;



public class SearchAddressDao {
	
	private Connection conn = null;
	private PreparedStatement sta = null;
	private ResultSet r = null;	
	
	public ArrayList<AddressBean> searchAddress(Integer usersId)
	{
		conn = Conn.getConn();
		String sql = "SELECT address.* FROM address WHERE users_id = ? ";
		ArrayList<AddressBean> addressList = new ArrayList<AddressBean>();
		
		try {
			
			sta = conn.prepareStatement(sql);
			sta.setObject(1, usersId);
			r = sta.executeQuery();
			while(r.next())
			{
				
			
				Integer addressExist = r.getInt("address_exist");
				String addressRoute = r.getString("address_route");
				Integer addressId = r.getInt("address_id");
				Integer ordersId = r.getInt("orders_id");
				String connectTel = r.getString("connect_tel");
				
				AddressBean addressBean = new AddressBean();
				
				addressBean.setAddressExist(addressExist);
				addressBean.setAddressRoute(addressRoute);
				addressBean.setAddressId(addressId);
				addressBean.setOrdersId(ordersId);
				addressBean.setConnectTel(connectTel);
				
				addressList.add(addressBean);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(r != null)
				{
					r.close();
				}
				if(sta != null)
				{
					sta.close();
				}
				if(conn != null)
				{
					conn.close();
				}
	} catch (SQLException e) {
		e.printStackTrace();
		
	}
}	
		return addressList;
	}
	
	
	public ArrayList<GoodsBean> searchGoods(Integer goods_id)
	{
		
		 conn = Conn.getConn();
		 String sql = "select * from goods where goods_id = ?";
		 ArrayList<GoodsBean> goodsinformation = new ArrayList<GoodsBean>();
		 try {

			 sta =  conn.prepareStatement(sql);
			 sta.setObject(1, goods_id);
			 r = sta.executeQuery();
			 while(r.next())
			 {
				 Integer goodsId =  r.getInt("goods_id");
				 Integer brandsId = r.getInt("brands_id");
				 Integer adminsId = r.getInt("admins_id");
				 String goodsName = r.getString("goods_name");
				 String goodsImg =  r.getString("goods_img");
				 Float goodsPrice = r.getFloat("goods_price");
				 Integer goodsStock = r.getInt("goods_stock");
				 String goodsDescription = r.getString("goods_description");
				 Integer goodsStatus = r.getInt("goods_status");
				 Integer goodsExist = r.getInt("goods_exist");
				 
				 GoodsBean goodsBean = new GoodsBean();
				 goodsBean.setGoodsId(goodsId);
				 goodsBean.setBrandsId(brandsId);
				 goodsBean.setAdminsId(adminsId);
				 goodsBean.setGoodsName(goodsName);
				 goodsBean.setGoodsImg(goodsImg);
				 goodsBean.setGoodsPrice(goodsPrice);
				 goodsBean.setGoodsDescription(goodsDescription);
				 goodsBean.setGoodsStatus(goodsStatus);
				 goodsBean.setGoodsExist(goodsExist);
				 goodsBean.setGoodsStock(goodsStock);
				  	 
				 goodsinformation.add(goodsBean);
			 } 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(r != null)
				{
					r.close();
				}
				if(sta != null)
				{
					sta.close();
				}
				if(conn != null)
				{
					conn.close();
				}
	} catch (SQLException e) {
		e.printStackTrace();
		
	}
}		
		return goodsinformation;
	}
	
	
	
	public ArrayList<CartBean> searchCart(Integer usersId)
	{
		conn = Conn.getConn();
		String sql = "SELECT  goods.goods_id,goods_name,goods_img,goods_price,goods_stock,goods_description,goods_status,goods_exist,admins_id,brands_id,cart_id,cart_date,goods_amount  from eshop.cart inner join eshop.goods on cart.goods_id = goods.goods_id WHERE  cart.users_id = ? order by cart_id desc ";
		ArrayList<CartBean> cartlist= new ArrayList<CartBean>();
		
		
		 try {
		sta = conn.prepareStatement(sql);
		sta.setObject(1, usersId);
		r = sta.executeQuery();
		while(r.next())
		{
			 Integer goodsId = r.getInt("goods_id");
			 Integer brandsId = r.getInt("brands_id");
			 Integer adminsId = r.getInt("admins_id");
			 String goodsName = r.getString("goods_name");
			 String goodsImg =  r.getString("goods_img");
			 Float goodsPrice = r.getFloat("goods_price");
			 Integer goodsStock = r.getInt("goods_stock");
			 String goodsDescription = r.getString("goods_description");
			 Integer goodsStatus = r.getInt("goods_status");
			 Integer goodsExist = r.getInt("goods_exist");
			 
			 GoodsBean goodsBean = new GoodsBean();//goodsBean
			 
			 goodsBean.setGoodsId(goodsId);
			 goodsBean.setBrandsId(brandsId);
			 goodsBean.setAdminsId(adminsId);
			 goodsBean.setGoodsName(goodsName);
			 goodsBean.setGoodsImg(goodsImg);
			 goodsBean.setGoodsPrice(goodsPrice);
			 goodsBean.setGoodsDescription(goodsDescription);
			 goodsBean.setGoodsStatus(goodsStatus);
			 goodsBean.setGoodsExist(goodsExist);
			 goodsBean.setGoodsStock(goodsStock);
			 
			 Integer goodsAmount = r.getInt("goods_amount");
			 Integer cartId = r.getInt("cart_id");
			 String cartDate = r.getString("cart_date");
			 CartBean cartBean = new CartBean();
			 cartBean.setGoodsAmount(goodsAmount);
			 cartBean.setCartId(cartId);
			 cartBean.setCartDate(cartDate);
			 cartBean.setGoods(goodsBean);
			 
			 cartlist.add(cartBean);

		}
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		 
		return cartlist;	
	}

}
